<template>
  <div class="app-container home">
    <div class="fr">
      <el-form :inline="true" :model="formInline" class="">
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="formInline.keyword" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="符号编码" prop="symbolCode">
          <el-input v-model="formInline.symbolCode" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="符号类型" prop="symbolType">
          <el-select v-model="formInline.symbolType" clearable placeholder="请选择" size="small">
            <el-option v-for="(item,index) in symbolTypeList" :key="'symbol-type'+index" :value="item.value"
              :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" size="small">搜索</el-button>
          <el-button type="primary" plain @click="handleReset" size="small">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="btn">
      <el-button type="primary" size="small" @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="loading" :data="tableData" :row-key="row=>row.id">
      <el-table-column label="序号" type="index" width="50" :index="indexMethod">
      </el-table-column>
      <el-table-column prop="symbolKey" label="符号">
        <template slot-scope="scope">
          <span v-if="scope.row.symbolType == 'TEXT'">{{ scope.row.symbolKey }}</span>
          <div v-else-if="scope.row.symbolType == 'FORMULA_DATA'" :style="{background: `url(${getImgJsonSrc(scope.row.symbolKey)})`,backgroundRepeat: 'no-repeat',
          width: '100%',
          height: '22px',
          backgroundSize: 'contain'}"></div>

          <div v-else-if="scope.row.symbolType == 'IMAGE'" :style="{background:`url(${getImgSrc(scope.row.symbolKey)})`,backgroundRepeat: 'no-repeat',
            width: '100%',
            height: '22px',
            backgroundSize: 'contain'}"></div>

          <!-- <img v-else-if="scope.row.symbolType == 'FORMULA_DATA'" src="@/assets/404_images/404.png"
          @load="setImageSize" />
          <img v-else-if="scope.row.symbolType == 'IMAGE'" src="@/assets/404_images/404.png"
          @load="setImageSize" /> -->
        </template>
      </el-table-column>
      <el-table-column prop="symbolCode" label="符号编码"> </el-table-column>
      <el-table-column prop="symbolDescribe" label="符号单位">
        <template slot-scope="scope">
          <span v-if="scope.row.symbolDescribeType == 'TEXT'">{{ scope.row.symbolDescribe }}</span>
          <div v-else-if="scope.row.symbolDescribeType == 'FORMULA_DATA'" :style="{background:`url(${getImgJsonSrc(scope.row.symbolDescribe)})` ,backgroundRepeat: 'no-repeat',
          width: '100%',
          height: '22px',
          backgroundSize: 'contain'}"></div>

          <div v-else-if="scope.row.symbolDescribeType == 'IMAGE'" :style="{background: `url(${getImgSrc(scope.row.symbolDescribe)})`,backgroundRepeat: 'no-repeat',
          width: '100%',
          height: '22px',
          backgroundSize: 'contain'}"></div>
          <!-- <img v-else-if="scope.row.symbolDescribeType == 'FORMULA_DATA'" :src="getImgJsonSrc(scope.row.symbolDescribe)"
            @load="setImageSize"/>
          <img v-else-if="scope.row.symbolDescribeType == 'IMAGE'" :src="getImgSrc(scope.row.symbolDescribe)"
          @load="setImageSize" /> -->
          <!-- <img v-else-if="scope.row.symbolType == 'FORMULA_DATA'" src="@/assets/404_images/404.png"
          @load="setImageSize" />
          <img v-else-if="scope.row.symbolType == 'IMAGE'" src="@/assets/404_images/404.png"
          @load="setImageSize" /> -->
        </template>
      </el-table-column>
      <el-table-column prop="symbolName" label="符号名称" />
      <el-table-column prop="symbolType" label="符号类型">
        <template slot-scope="scope">
          {{ getLabelByValue(symbolTypeList,scope.row.symbolType) }}
        </template>
      </el-table-column>
      <el-table-column prop="symbolValue" label="符号右值" />
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="p20 fr" v-if="total > 0" :background="true" :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize" :layout="'total, sizes, prev, pager, next, jumper'"
      :page-sizes="[10, 30, 50, 100]" :total="total" @size-change="handleSizeChange"
      @current-change="handlepageNumChange" />
    <SymbolDialog :datas="symbolDatas" v-if="dialogVisible" :type="type" @on-close="handleDialogClose"
      @on-sure="onSearch" />
  </div>
</template>

<script>
  import SymbolDialog from '@/components/Editor/symbolDialog.vue'
  import { getAlgorithmSymbolList, deleteAlgorithmSymbol } from "@/api/symbol";
  import pg from "@/assets/404_images/404.png"
  export default {
    name: "symbolConfig",
    components: { SymbolDialog },
    data() {
      return {
        formInline: {
          keyword: "",
          symbolCode: "",
          symbolType: ''
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        loading: false,
        total: 0,
        tableData: [],
        dialogVisible: false,
        symbolDatas: {},
        type: 'add',
        symbolTypeList: [
          {
            label: '图片符号',
            value: 'IMAGE'
          },
          {
            label: '图片+LATEX符号',
            value: 'FORMULA_DATA'
          },
          {
            label: '纯文本',
            value: 'TEXT'
          }
        ]
      };
    },
    methods: {
     async setImageSize(event){
      console.log("图片属性-1")
      console.log(event)
        event.path[0].height = event.path[0].height/2;
        event.path[0].width = event.path[0].width/2;
        console.log("图片属性-2")
        console.log(event)
      },
      indexMethod(index) {
        return (
          (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
        );
      },
      getImgSrc(ossId) {
        return window.location.origin + '/prod-api/editor/file/download/' + ossId
        // return 'http://172.16.10.99/dev-api/editor/file/download/' +ossId
      },
      getImgJsonSrc(data) {
        const obj = JSON.parse(data);
        return obj.url;
      },
      getLabelByValue(arr, value) {
        const obj = arr.find(item => item.value === value)
        if (obj) {
          return obj.label
        }
        return value
      },
      async onSearch() {
        try {
          this.loading = true;
          const res = await getAlgorithmSymbolList({
            ...this.queryParams,
            ...this.formInline,
          });
          this.tableData = res.data.list;
          this.total = res.data.count;
        } catch (error) {
          console.debug(error);
        } finally {
          this.loading = false;
        }
      },
      handleReset() {
        this.formInline = {
          keyword: "",
          symbolType: "",
          symbolNamespace: '',
          symbolType: ''
        };
        this.onSearch();
      },
      handleSearch() {
        this.queryParams.pageNum = 1;
        this.onSearch();
      },
      handleSizeChange(val) {
        if (this.queryParams.pageSize * val > this.total) {
          this.queryParams.pageNum = 1;
        }
        this.onSearch();
      },
      handlepageNumChange() {
        this.onSearch();
      },
      handleAdd() {
        this.symbolDatas = {}
        this.type = 'add'
        this.dialogVisible = true
      },
      handleView(row) {
        this.symbolDatas = row
        this.type = 'view'
        this.dialogVisible = true
      },
      handleEdit(row) {
        this.symbolDatas = row
        this.type = 'edit'
        this.dialogVisible = true
      },
      handleDelete(id) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAlgorithmSymbol(id).then(res => {
            this.$message({ type: 'success', message: '删除成功!' });
            this.handleSearch();
          })
        }).catch(() => {
          this.$message({ type: 'info', message: '已取消删除' });
        });
      },
      handleDialogClose() {
        this.dialogVisible = false
      }
    },
    mounted() {
      this.handleSearch();
    },
  };
</script>
<style scoped>
  .btn {
    padding: 16px;
  }
</style>